<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
	用户
 */
class User extends MY_Controller
{

	/*
		构造函数
	 */
	public function __construct()
	{
		parent::__construct();
      		$this->load->model('M_user');
	}

	/*
		注册
	 */
	public function register()
	{

		$department_id = $this->input->post('department_id');					//部门名称
		$user_name = $this->input->post('user_name');						//用户姓名
		$user_account = $this->input->post('user_account');					//用户账号
		$user_pwd = $this->input->post('user_pwd');						//用户密码
		$post_id = $this->input->post('post_id');							//职务名称
		$role_id = $this->input->post('role_id');							//角色id
		$radio_id = $this->input->post('radio_id');						//广播id
		$user_weight = $this->input->post('user_weight');					//用户权重
		$is_leader = $this->input->post('is_leader');						//是否领导
		$user_tel = $this->input->post('user_tel');						//用户手机
		$user_email = $this->input->post('user_email');						//用户邮箱

		$user_pwd = md5($user_pwd);
		$list = $this->db->select('user_id')->from('t_user')->where(['user_account'=>$user_account])->get()->row_array();
		if($list)
		{
			$res['code'] = 2;
			$res['message'] = "账号已存在";
		}else{
			$result = $this->db->insert('t_user',['user_account'=>$user_account,'user_pwd'=>$user_pwd,'user_name'=>$user_name,'user_tel'=>$user_tel,'user_email'=>$user_email,'is_leader'=>$is_leader,'role_id'=>$role_id,'radio_id'=>$radio_id,'user_weight'=>$user_weight,'department_id'=>$department_id,'post_id'=>$post_id]);
			if($result)
			{
				$res['code'] = 0;
				$res['message'] = "添加成员成功";
			}else{
				$res['code'] = 1;
				$res['message'] = "添加成员失败";
			}
		}

		return json_encode($res);

	}

	/*
		登录
	 */
	public function login()
	{

		$user_account = $this->input->post('user_account');		//账号
		$user_pwd = $this->input->post('user_pwd');			//密码
		
		$user_pwd = md5($user_pwd);
		$result = $this->db->select('user_id')->from('t_user')->where(['user_account'=>$user_account,'user_pwd'=>$user_pwd])->get()->row_array();
		if($result)
		{
			$res['code'] = 0;
			$res['message'] = "登录成功";
			$res['data']['user_id'] = $result['user_id'];
		}else{
			$res['code'] = 1;
			$res['message'] = "登录失败";
		}

		return json_encode($res);

	}

}